package com.goldsign.pay.provider.msg.rabbit.producer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.goldsign.pay.provider.msg.controller.MsgController;


@Component
/**消息回调需要实现  RabbitTemplate.ConfirmCallback{*/
public class RbtProducer implements RabbitTemplate.ConfirmCallback{
	private Logger LOGGER = LoggerFactory.getLogger(MsgController.class);
	
	@Autowired
	private AmqpTemplate amqpTemplate;
	
	/**默认交换机*/ 
	public void send(String exchange, String routingKey, String content) {
    	LOGGER.info(" ==> exchange:{} routingKey:{}  content:{}" , exchange, routingKey,content);
    	this.amqpTemplate.convertAndSend(exchange, routingKey, content);
    }
	
	/**发到指定交换机*/
	public void send(String routingKey, String content) {
    	LOGGER.info(" ==> routingKey:{}  content:{}" , routingKey,content);
    	this.amqpTemplate.convertAndSend(routingKey, content);
    }
	
	
	
    /**回调::可选*/ 
    /**如果需要在生产者需要消息发送后的回调，需要对rabbitTemplate设置ConfirmCallback对象*/
    @Override  
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {  
        System.out.println(" 回调id:" + correlationData);  
        if (ack) {  
        	LOGGER.info("消息成功消费");  
        } else {  
        	LOGGER.info("消息消费失败:" + cause);  
        }  
    } 
}
